<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="记录编码(根据录入日期进行序号编排)" prop="finaCode">
        <el-input
          v-model="queryParams.finaCode"
          placeholder="请输入记录编码(根据录入日期进行序号编排)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="finaDate">
        <el-date-picker clearable
          v-model="queryParams.finaDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="一级科目" prop="subId1">
        <el-input
          v-model="queryParams.subId1"
          placeholder="请输入一级科目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="科目一名称" prop="subName1">
        <el-select v-model="queryParams.subName1" placeholder="请选择科目一名称" clearable>
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="二级科目" prop="subId2">
        <el-input
          v-model="queryParams.subId2"
          placeholder="请输入二级科目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="科目二名称" prop="subName2">
        <el-select v-model="queryParams.subName2" placeholder="请选择科目二名称" clearable>
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="三级科目" prop="subId3">
        <el-input
          v-model="queryParams.subId3"
          placeholder="请输入三级科目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="科目三名称" prop="subName3">
        <el-select v-model="queryParams.subName3" placeholder="请选择科目三名称" clearable>
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="四级科目" prop="subId4">
        <el-input
          v-model="queryParams.subId4"
          placeholder="请输入四级科目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="科目四名称" prop="subName4">
        <el-select v-model="queryParams.subName4" placeholder="请选择科目四名称" clearable>
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="显示顺序" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入显示顺序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="摘要" prop="finaAbstract">
        <el-input
          v-model="queryParams.finaAbstract"
          placeholder="请输入摘要"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经办人" prop="finaOperatorId">
        <el-select v-model="queryParams.finaOperatorId" placeholder="请选择经办人" clearable>
          <el-option
            v-for="dict in dict.type.cash_opter"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="收入-蓝色" prop="finaIn">
        <el-input
          v-model="queryParams.finaIn"
          placeholder="请输入收入-蓝色"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支出-红色" prop="finaOut">
        <el-input
          v-model="queryParams.finaOut"
          placeholder="请输入支出-红色"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="余额" prop="finaBalance">
        <el-input
          v-model="queryParams.finaBalance"
          placeholder="请输入余额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择部门状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_common_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="经办人名" prop="finaOperatorName">
        <el-input
          v-model="queryParams.finaOperatorName"
          placeholder="请输入经办人名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="cusName">
        <el-input
          v-model="queryParams.cusName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="apprName">
        <el-input
          v-model="queryParams.apprName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批状态 0：待审批；1：通过；2：不通过" prop="apprStatus">
        <el-select v-model="queryParams.apprStatus" placeholder="请选择审批状态 0：待审批；1：通过；2：不通过" clearable>
          <el-option
            v-for="dict in dict.type.appr_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审批时间" prop="apprTime">
        <el-date-picker clearable
          v-model="queryParams.apprTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择审批时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['fina:cash:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['fina:cash:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fina:cash:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fina:cash:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cashList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="出纳id" align="center" prop="finaId" />
      <el-table-column label="记录编码(根据录入日期进行序号编排)" align="center" prop="finaCode" />
      <el-table-column label="" align="center" prop="finaDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finaDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="一级科目" align="center" prop="subId1" />
      <el-table-column label="科目一名称" align="center" prop="subName1" />
      <el-table-column label="二级科目" align="center" prop="subId2" />
      <el-table-column label="科目二名称" align="center" prop="subName2" />
      <el-table-column label="三级科目" align="center" prop="subId3" />
      <el-table-column label="科目三名称" align="center" prop="subName3" />
      <el-table-column label="四级科目" align="center" prop="subId4" />
      <el-table-column label="科目四名称" align="center" prop="subName4" />
      <el-table-column label="显示顺序" align="center" prop="orderNum" />
      <el-table-column label="摘要" align="center" prop="finaAbstract" />
      <el-table-column label="经办人" align="center" prop="finaOperatorId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cash_opter" :value="scope.row.finaOperatorId"/>
        </template>
      </el-table-column>
      <el-table-column label="收入-蓝色" align="center" prop="finaIn" />
      <el-table-column label="支出-红色" align="center" prop="finaOut" />
      <el-table-column label="余额" align="center" prop="finaBalance" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="部门状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_common_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="经办人名" align="center" prop="finaOperatorName" />
      <el-table-column label="客户名称" align="center" prop="cusName" />
      <el-table-column label="客户名称" align="center" prop="apprName" />
      <el-table-column label="审批状态 0：待审批；1：通过；2：不通过" align="center" prop="apprStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.appr_status" :value="scope.row.apprStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="审批时间" align="center" prop="apprTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.apprTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fina:cash:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fina:cash:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改出纳管理列对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="记录编码(根据录入日期进行序号编排)" prop="finaCode">
          <el-input v-model="form.finaCode" placeholder="请输入记录编码(根据录入日期进行序号编排)" />
        </el-form-item>
        <el-form-item label="" prop="finaDate">
          <el-date-picker clearable
            v-model="form.finaDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="一级科目" prop="subId1">
          <el-input v-model="form.subId1" placeholder="请输入一级科目" />
        </el-form-item>
        <el-form-item label="科目一名称" prop="subName1">
          <el-select v-model="form.subName1" placeholder="请选择科目一名称">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="二级科目" prop="subId2">
          <el-input v-model="form.subId2" placeholder="请输入二级科目" />
        </el-form-item>
        <el-form-item label="科目二名称" prop="subName2">
          <el-select v-model="form.subName2" placeholder="请选择科目二名称">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="三级科目" prop="subId3">
          <el-input v-model="form.subId3" placeholder="请输入三级科目" />
        </el-form-item>
        <el-form-item label="科目三名称" prop="subName3">
          <el-select v-model="form.subName3" placeholder="请选择科目三名称">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="四级科目" prop="subId4">
          <el-input v-model="form.subId4" placeholder="请输入四级科目" />
        </el-form-item>
        <el-form-item label="科目四名称" prop="subName4">
          <el-select v-model="form.subName4" placeholder="请选择科目四名称">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="显示顺序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入显示顺序" />
        </el-form-item>
        <el-form-item label="摘要" prop="finaAbstract">
          <el-input v-model="form.finaAbstract" placeholder="请输入摘要" />
        </el-form-item>
        <el-form-item label="经办人" prop="finaOperatorId">
          <el-select v-model="form.finaOperatorId" placeholder="请选择经办人">
            <el-option
              v-for="dict in dict.type.cash_opter"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收入-蓝色" prop="finaIn">
          <el-input v-model="form.finaIn" placeholder="请输入收入-蓝色" />
        </el-form-item>
        <el-form-item label="支出-红色" prop="finaOut">
          <el-input v-model="form.finaOut" placeholder="请输入支出-红色" />
        </el-form-item>
        <el-form-item label="余额" prop="finaBalance">
          <el-input v-model="form.finaBalance" placeholder="请输入余额" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="部门状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_common_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
        <el-form-item label="经办人名" prop="finaOperatorName">
          <el-input v-model="form.finaOperatorName" placeholder="请输入经办人名" />
        </el-form-item>
        <el-form-item label="客户名称" prop="cusName">
          <el-input v-model="form.cusName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="客户名称" prop="apprName">
          <el-input v-model="form.apprName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="审批状态 0：待审批；1：通过；2：不通过">
          <el-radio-group v-model="form.apprStatus">
            <el-radio
              v-for="dict in dict.type.appr_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审批时间" prop="apprTime">
          <el-date-picker clearable
            v-model="form.apprTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择审批时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCash, getCash, delCash, addCash, updateCash } from "@/api/fina/cash";

export default {
  name: "Cash",
  dicts: ['sys_common_status', 'appr_status', 'cash_opter'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出纳管理列表格数据
      cashList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        finaCode: null,        finaDate: null,        subId1: null,        subName1: null,        subId2: null,        subName2: null,        subId3: null,        subName3: null,        subId4: null,        subName4: null,        orderNum: null,        finaAbstract: null,        finaOperatorId: null,        finaIn: null,        finaOut: null,        finaBalance: null,        status: null,        finaOperatorName: null,        cusName: null,        apprName: null,        apprStatus: null,        apprTime: null      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        finaCode: [
          { required: true, message: "记录编码(根据录入日期进行序号编排)不能为空", trigger: "blur" }
        ],        finaDate: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],        subName1: [
          { required: true, message: "科目一名称不能为空", trigger: "change" }
        ],        subName2: [
          { required: true, message: "科目二名称不能为空", trigger: "change" }
        ],        subName3: [
          { required: true, message: "科目三名称不能为空", trigger: "change" }
        ],        subName4: [
          { required: true, message: "科目四名称不能为空", trigger: "change" }
        ],        finaAbstract: [
          { required: true, message: "摘要不能为空", trigger: "blur" }
        ],        finaOperatorId: [
          { required: true, message: "经办人不能为空", trigger: "change" }
        ],      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询出纳管理列列表 */
    getList() {
      this.loading = true;
      listCash(this.queryParams).then(response => {
        this.cashList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        finaId: null,        finaCode: null,        finaDate: null,        subId1: null,        subName1: null,        subId2: null,        subName2: null,        subId3: null,        subName3: null,        subId4: null,        subName4: null,        orderNum: null,        finaAbstract: null,        finaOperatorId: null,        finaIn: null,        finaOut: null,        finaBalance: null,        remark: null,        status: "0",        delFlag: null,        createBy: null,        createTime: null,        updateBy: null,        updateTime: null,        finaOperatorName: null,        cusName: null,        apprName: null,        apprStatus: "0",        apprTime: null      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.finaId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加出纳管理列";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const finaId = row.finaId || this.ids
      getCash(finaId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改出纳管理列";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.finaId != null) {
            updateCash(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCash(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const finaIds = row.finaId || this.ids;
      this.$modal.confirm('是否确认删除出纳管理列编号为"' + finaIds + '"的数据项？').then(function() {
        return delCash(finaIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fina/cash/export', {
        ...this.queryParams
      }, `cash_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
